const {env, article, food, user, collect} = require('./config')

wx.cloud.init({
    env
})

//初始化数据库
const db = wx.cloud.database();
const _ = db.command;

const articleCollection = db.collection(article);
const foodCollection = db.collection(food);
const userCollection = db.collection(user);
const collectCollection = db.collection(collect);


/**
 * 获取文章列表
 * @param  {number}   category     类型（分类）
 */
const getArticleList = async (category) => {
    return await articleCollection.where({
        category,
        swiper: false
    }).orderBy('_createTime', 'desc').get()
}

/**
 * 获取食物列表
 * @param  {number}   category     类型（分类）
 */
const getFoodList = async (category) => {
    return await foodCollection.where({
        category,
    }).orderBy('_createTime', 'desc').get()
}

/**
 * 获取轮播图内容
 */
const getSwiper = async () => {
    return await articleCollection.where({
        swiper: true
    }).get()
}


/**
 * 获取详情数据
 * @param  {string}   collection   集合名称
 * @param  {string}   id
 */
const getDetail = async (collection, id) => {
    return await db.collection(collection)
        .doc(id).get()
}


/**
 * 获取点击量前8的数据
 * @param  {string}   collection   集合名称
 */
const getHotList = async (collection) => {
    return await db.collection(collection)
        .orderBy('hits', 'desc')
        .limit(8)
        .get()
}


/**
 * 多字段查询(待优化)
 * @param  {string}   collection   集合名称
 * @param  {string}   keyword      搜索关键词
 */
const searchResult = async (collection, keyword) => {
    return await db.collection(collection).where(_.or([{
        title: db.RegExp({
            regexp: '.*' + keyword
        })
    },
        {
            level: db.RegExp({
                regexp: '.*' + keyword
            })
        },
        {
            foodName: db.RegExp({
                regexp: '.*' + keyword
            })
        }
    ])).get()
}


/**
 * 用户注册
 * @param  {string}   avatarUrl    头像
 * @param  {string}   nickName     昵称
 * @param  {number}   gender       性别
 * @param  {number}   width        身高
 * @param  {number}   height       体重
 */
const register = async (avatarUrl, nickName, gender, width = 0, height = 0) => {
    return await userCollection.add({
        data: {
            avatarUrl,
            nickName,
            gender,
            width,
            height
        }
    })
}


/**
 * 查询用户信息
 * @param  {string}   _openid    用户ID
 */
const getUser = async (_openid) => {
    return await userCollection.where({
        _openid
    }).get()
}


/**
 * 根据视频ID判断是否收藏
 * @param  {string}   thisCategory   收藏对象类别（article,food）
 * @param  {string}   openid         收藏者OPENID
 * @param  {string}   thisID         收藏对象ID
 */
const queryCollect = async (thisCategory, openid, thisID) => {
    return await collectCollection.where({
        thisCategory,
        openid,
        thisID
    }).get()
}


/**
 * 获取收藏列表
 * @param  {string}   thisCategory   收藏对象类别（article,food）
 * @param  {string}   openid         用户ID
 */
const getCollectList = async (thisCategory, openid) => {
    return await collectCollection.where({
        thisCategory,
        openid
    }).get()
}


module.exports = {
    getArticleList,
    getFoodList,
    getSwiper,
    getDetail,
    getHotList,
    searchResult,
    register,
    getUser,
    queryCollect,
    getCollectList
}